package com.itheima.springbootquickstart.config;

import com.itheima.pojo.Country;
import com.itheima.pojo.Province;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * CommonConfig
 *
 * @author zhaokf
 * @date 2024/11/14
 */
@Configuration
public class CommonConfig {
    //如果配置了相关信息,设置相关属性
    @Bean("Country")
    @ConditionalOnProperty(prefix = "country", name = {"name", "system"})
    Country country(@Value("${country.name}") String name, @Value("${country.system}") String system) {
        Country country = new Country();
        country.setName(name);
        country.setSystem(system);
        return country;
    }

    @Bean("Province")
    @ConditionalOnMissingBean(Country.class)
    Province province() {
        return new Province();
    }
}
